package com.wsoft.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.annotation.InitRedisCache;
import com.wsoft.core.utils.SqlAuthDisableHolder;
import com.wsoft.system.entity.SysDataPermEntity;
import com.wsoft.system.entity.SysRolePermRelationEntity;
import com.wsoft.system.mapper.SysRolePermRelationMapper;
import com.wsoft.system.query.role.perm.RoleDataPermQuery;
import com.wsoft.system.service.ISysDataPermService;
import com.wsoft.system.service.ISysRolePermRelationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zgk
 * @since 2024年09月23日 09:18:44
 */
@Service
public class SysRolePermRelationServiceImpl extends ServiceImpl<SysRolePermRelationMapper, SysRolePermRelationEntity> implements ISysRolePermRelationService {

    @Resource
    private ISysDataPermService dataPermService;

    @Override
    @GetRedisCache(type = RedisKeyConstant.ROLE_DATA_PERM_CACHE,
                   value = RedisKeyConstant.REDIS_SELF)
    public List<SysDataPermEntity> getDataPermListByRoleId(Long roleId) {
        SqlAuthDisableHolder.open();
        QueryWrapper<SysRolePermRelationEntity> query = new QueryWrapper<>();
        query.lambda().eq(SysRolePermRelationEntity::getRoleId,roleId);
        List<SysRolePermRelationEntity> list = list(query);
        List<Long> permIds = list.stream().map(SysRolePermRelationEntity::getPermId).collect(Collectors.toList());
        List<SysDataPermEntity> dataPermList = new ArrayList<>();
        if (CollUtil.isNotEmpty(permIds)){
            permIds.forEach(e->{
                SysDataPermEntity entity = dataPermService.getDataPerm(e);
                dataPermList.add(entity);
            });
        }
        SqlAuthDisableHolder.close();
        return dataPermList;
    }

//    @Override
//    @GetRedisCache(type = RedisKeyConstant.MEMBER_DATA_PERM,
//            value = RedisKeyConstant.REDIS_SELF)
//    public List<SysDataPermEntity> getDataPermListByMemberId(Long memberId) {
//        SqlAuthDisableHolder.open();
//        List<SysRoleEntity> list = memberOrgRoleRelationService.getRoleListByMember(memberId);
//        List<Long> roleIds = list.stream().map(BaseEntity::getId).collect(Collectors.toList());
//                QueryWrapper<SysRolePermRelationEntity> query = new QueryWrapper<>();
//            query.lambda().in(SysRolePermRelationEntity::getRoleId, roleIds);
//        List<Long> permIds = list(query).stream().map(SysRolePermRelationEntity::getPermId).collect(Collectors.toList());
//        if (permIds.size() == 0){
//            return new ArrayList<>();
//        }
//        List<SysDataPermEntity> dataPermList = dataPermService.listByIds(permIds);
//        SqlAuthDisableHolder.close();
//        return dataPermList;
//    }

    @Override
    @InitRedisCache(type = RedisKeyConstant.ROLE_DATA_PERM_CACHE,
                    value = "roleId")
    @Transactional(rollbackFor = Exception.class)
    public boolean insertRelationByRoleId(RoleDataPermQuery query) {
        //清空角色数据权限关系
        deleteByRoleId(query.getRoleId());
        //新增角色数据权限关系
        if (query.getPermIds() != null && query.getPermIds().size() > 0) {
            List<SysRolePermRelationEntity> list = new ArrayList<>();
            for (Long permId : query.getPermIds()) {
                SysRolePermRelationEntity relation = new SysRolePermRelationEntity();
                relation.setRoleId(query.getRoleId());
                relation.setPermId(permId);
                list.add(relation);
            }
            return saveBatch(list);
        }
        return true;
    }
    private void deleteByRoleId(Long roleId){
        QueryWrapper<SysRolePermRelationEntity> query = new QueryWrapper<>();
        query.lambda().eq(SysRolePermRelationEntity::getRoleId,roleId);
        remove(query);
    }
}
